package com.example.service.impl;

import java.io.ByteArrayInputStream;



import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import com.example.controller.BaseFrontController;
import com.example.pojo.Quexianzhiding;
import com.example.service.ExportService;
import com.example.utils.ExcelFormatUtil;



@Service
public class ExportServiceImpl implements ExportService{
	Logger logger = LoggerFactory.getLogger(ExportServiceImpl.class);

	@Override
	public ResponseEntity<byte[]> exportExcelIsXQ(HttpServletRequest request,
			HttpServletResponse response,
			List<Quexianzhiding> list) {
		// TODO Auto-generated method stub
		if(list!=null||list.size()>0) {
			
		
			try {
				logger.info(">>>>>>>>>>开始导出excel>>>>>>>>>>");
				
				// 造几条数据
				
				BaseFrontController baseFrontController = new BaseFrontController();
				return baseFrontController.buildResponseEntity(export2((List<Quexianzhiding>) list), "消缺任务制定表.xls");
			} catch (Exception e) {
				e.printStackTrace();
				logger.error(">>>>>>>>>>导出excel 异常，原因为：" + e.getMessage());
			}
		}
		return null;
		
	}
	private InputStream export2(List<Quexianzhiding> list) {
		logger.info(">>>>>>>>>>>>>>>>>>>>开始进入导出方法>>>>>>>>>>");
		ByteArrayOutputStream output = null;
		InputStream inputStream1 = null;
		SXSSFWorkbook wb = new SXSSFWorkbook(1000);// 保留1000条数据在内存中
		SXSSFSheet sheet = wb.createSheet();
		// 设置报表头样式
		CellStyle header = ExcelFormatUtil.headSytle(wb);// cell样式
		CellStyle content = ExcelFormatUtil.contentStyle(wb);// 报表体样式
		
		// 每一列字段名
		String[] strs = new String[] { "任务编号", "任务名称", "工作单据", "下发人"/* ,"下发时间" */, "任务状态",
				/* "任务完成时间", */"任务是否取消"/*
														 * , "杆塔编号", "工作单据", "缺陷级别", "缺陷类型"
														 *//* ,"发现人","发现时间","任务下发人","下发时间","完好率","缺陷描述" */};
		
		// 字段名所在表格的宽度
		int[] ints = new int[] { 5000, 5000,5000, 5000,5000, 5000};
		
		// 设置表头样式
		ExcelFormatUtil.initTitleEX(sheet, header, strs, ints);
		logger.info(">>>>>>>>>>>>>>>>>>>>表头样式设置完成>>>>>>>>>>");
		
		if (list != null && list.size() > 0) {
			logger.info(">>>>>>>>>>>>>>>>>>>>开始遍历数据组装单元格内容>>>>>>>>>>");
			for (int i = 0; i < list.size(); i++) {
				Quexianzhiding user = list.get(i);

				SXSSFRow row = sheet.createRow(i + 1);
				int j = 0;

				SXSSFCell cell = row.createCell(j++);
				cell.setCellValue(user.getRwid()); // 任务编号
				cell.setCellStyle(content);

				cell = row.createCell(j++);
				cell.setCellValue(user.getRwname()); //"任务名称"
				cell.setCellStyle(content);
				
				cell = row.createCell(j++);
				cell.setCellValue(user.getRwdj()); //"工作单据"
				cell.setCellStyle(content);
				
				cell = row.createCell(j++);
				cell.setCellValue(user.getXfuser()); //"下发人"
				cell.setCellStyle(content);
				
				/*
				 * cell = row.createCell(j++); cell.setCellValue(user.getXfdate()); //"下发时间"
				 * cell.setCellStyle(content);
				 */
				
				cell = row.createCell(j++);
				cell.setCellValue(user.getRwzt()); //"任务状态"
				cell.setCellStyle(content);
				
				/*
				 * cell = row.createCell(j++); cell.setCellValue(user.getWcsj()); //"任务完成时间"
				 * cell.setCellStyle(content);
				 */
				
				cell = row.createCell(j++);
				cell.setCellValue(user.getIsqx()); //"任务是否取消"
				cell.setCellStyle(content);

				/*
				 * cell = row.createCell(j++); cell.setCellValue(user.getGtid()); //"杆塔编号"
				 * cell.setCellStyle(content);
				 * 
				 * cell = row.createCell(j++); cell.setCellValue(user.getWordj()); //"工作单据"
				 * cell.setCellStyle(content);
				 * 
				 * cell = row.createCell(j++); cell.setCellValue(user.getQxlv()); //"缺陷级别"
				 * cell.setCellStyle(content);
				 * 
				 * cell = row.createCell(j++); cell.setCellValue(user.getQxtype()); //"缺陷类型"
				 * cell.setCellStyle(content);
				 */
				
				/*
				 * cell = row.createCell(j++); cell.setCellValue(user.getFindUser()); //"发现人"
				 * cell.setCellStyle(content);
				 */
				
				/*
				 * cell = row.createCell(j++); cell.setCellValue(user.getFindtime()); //"发现时间"
				 * cell.setCellStyle(content);
				 */
				
				/*
				 * cell = row.createCell(j++); cell.setCellValue(user.getXiauser()); //"任务下发人"
				 * cell.setCellStyle(content);
				 */
				
				/*
				 * cell = row.createCell(j++); cell.setCellValue(user.getXiaDate()); //"下发时间"
				 * cell.setCellStyle(content);
				 */
				
				/*
				 * cell = row.createCell(j++); cell.setCellValue(user.getRate()); //"完好率"
				 * cell.setCellStyle(content);
				 */
				
				/*
				 * cell = row.createCell(j++); cell.setCellValue(user.getQxmiaoshu()); //"缺陷描述"
				 * cell.setCellStyle(content);
				 */

			}
			logger.info(">>>>>>>>>>>>>>>>>>>>结束遍历数据组装单元格内容>>>>>>>>>>");
		}
		try {
			output = new ByteArrayOutputStream();
			wb.write(output);
			inputStream1 = new ByteArrayInputStream(output.toByteArray());
			output.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			try {
				if (output != null) {
					output.close();
					if (inputStream1 != null)
						inputStream1.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		return inputStream1;
	}
	@Override
	public void lalla() {
		// TODO Auto-generated method stub
		
	}
	
	
}
